// pages/group_forward/group_forward.js
var common = require("../../common/common.js").common;

Page({
    /**
   * 页面的初始数据
   */
    data: {
        choose: 1,
        money: 0,
        put_forward: "",
        service: 0,
        serviceType: 0,
        service_money: 0,
        payee_name: "",
        account1: "",
        account2: "",
        accountOwner: "",
        info: [],
        bank_name: "",
        putForward: false,
        tmplIds: [],
        subscribeMessage: true
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        this.getService();
        this.setData({
            money: options.money
        });
        this.getTempIds();
        this.readInfo();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    /**
   * 用户点击右上角分享
   */
    // onShareAppMessage: function () {
    // },
    choose: function(e) {
        var id = e.currentTarget.dataset.id;
        this.setData({
            choose: id
        });
    },
    putmoney: function(e) {
        var money = e.detail.value;
        money = money.replace(/[^\d.]/g, "").replace(/\.{2,}/g, ".").replace(".", "$#$").replace(/\./g, "").replace("$#$", ".").replace(/^(\-)*(\d+)\.(\d\d).*$/, "$1$2.$3").replace(/^\./g, "");
        var serviceType = this.data.serviceType;
        var service = this.data.service;
        var service_money = this.data.service_money;
        if (serviceType == 0) {
            service_money = service;
        } else {
            if (money == "") {
                money = 0;
            }
            service_money = parseFloat(money) * parseFloat(service / 100);
            if (money == 0) {
                money = "";
            }
        }
        this.setData({
            put_forward: money,
            service_money: service_money.toFixed(2)
        });
    },
    all: function(e) {
        var money = e.currentTarget.dataset.money;
        var serviceType = this.data.serviceType;
        var service = this.data.service;
        var service_money = this.data.service_money;
        if (serviceType == 0) {
            service_money = service;
        } else {
            service_money = parseFloat(money) * parseFloat(service / 100);
        }
        this.setData({
            put_forward: money,
            service_money: service_money.toFixed(2)
        });
    },
    getPayeeName: function(e) {
        this.setData({
            payee_name: e.detail.value
        });
    },
    getAccount1: function(e) {
        this.setData({
            account1: e.detail.value
        });
    },
    getAccount2: function(e) {
        this.setData({
            account2: e.detail.value
        });
    },
    getAccountOwner(e) {
        this.setData({
            accountOwner: e.detail.value
        });
    },
    getBankName(e) {
        this.setData({
            bank_name: e.detail.value
        });
    },
    putForward: function() {
        let that = this;
        that.putForward_ch();
        if (that.data.subscribeMessage) {
            wx.requestSubscribeMessage({
                tmplIds: that.data.tmplIds,
                success(res) {},
                fail(err) {}
            });
        }
    },
    putForward_ch: function() {
        var openid = wx.getStorageSync("user").openid;
        var that = this;
        //禁用提现按钮
                that.setData({
            putForward: true
        });
        var put_forward = that.data.put_forward;
        var payee_name = that.data.payee_name;
        var type = that.data.choose;
        var accountOwner = that.data.accountOwner;
        var account = "";
        var bank_name = that.data.bank_name;
        var all_money = that.data.money;
        if (!parseFloat(put_forward)) {
            wx.showModal({
                title: "提示",
                content: "请输入可提现的佣金！"
            });
            //解禁提现按钮
                        that.setData({
                putForward: false
            });
            return false;
        }
        if (parseFloat(put_forward) > parseFloat(all_money)) {
            wx.showModal({
                title: "提示",
                content: "提现金额超出可提现佣金！"
            });
            //解禁提现按钮
                        that.setData({
                putForward: false
            });
            return false;
        }
        if (type == 2) {
            account = that.data.account1;
        }
        if (type == 3) {
            account = that.data.account2;
        }
        if (payee_name == "" && type == 3) {
            wx.showModal({
                title: "提示",
                content: "请填写姓名！"
            });
            //解禁提现按钮
                        that.setData({
                putForward: false
            });
            return false;
        }
        if (account == "" && type == 2) {
            wx.showModal({
                title: "提示",
                content: "请填写支付宝账号！"
            });
            //解禁提现按钮
                        that.setData({
                putForward: false
            });
            return false;
        }
        if (accountOwner == "" && type == 2) {
            wx.showModal({
                title: "提示",
                content: "请填写支付宝收款人姓名！"
            });
            //解禁提现按钮
                        that.setData({
                putForward: false
            });
            return false;
        }
        if (account == "" && type == 3) {
            wx.showModal({
                title: "提示",
                content: "请填写银行卡号！"
            });
            //解禁提现按钮
                        that.setData({
                putForward: false
            });
            return false;
        }
        if (bank_name == "" && type == 3) {
            wx.showModal({
                title: "提示",
                content: "请填写银行名称！"
            });
            //解禁提现按钮
                        that.setData({
                putForward: false
            });
            return false;
        }
        common.request({
            endpoint: "Apply/financeapply/broker_submit",
            method: "post",
            data: {
                openid: openid,
                price: put_forward,
                account: account,
                payee_name: type == 3 ? payee_name : accountOwner,
                type: type,
                bank_name: bank_name
            },
            header: {
                "content-type": "application/json"
            },
            success: res => {
                wx.showToast({
                    title: res.message,
                    icon: "none"
                });
                setTimeout(() => {
                    wx.redirectTo({
                        url: "/leaderPerson/pages/center/center"
                    });
                }, 1500);
            },
            fail: res => {
                wx.showModal({
                    title: "提示",
                    content: res.message
                });
                //解禁提现按钮
                                that.setData({
                    putForward: false
                });
            }
        });
    },
    getService: function() {
        var that = this;
        common.request({
            endpoint: "Apply/financeapply/brokerage",
            method: "post",
            header: {
                "content-type": "application/json"
            },
            success: res => {
                that.setData({
                    service: res.extra.money_service,
                    serviceType: res.extra.type,
                    wait_bro: res.extra.wait_bro
                });
            },
            fail: res => {}
        });
    },
    readInfo: function() {
        var that = this;
        var openid = wx.getStorageSync("user").openid;
        common.request({
            endpoint: "Apply/financeapply/readinfo",
            method: "post",
            data: {
                openid: openid
            },
            header: {
                "content-type": "application/json"
            },
            success: res => {
                var info = res.extra.trans_type;
                that.setData({
                    account1: res.extra.trans_type[1].account,
                    payee_name: res.extra.trans_type[2].payee_name,
                    accountOwner: res.extra.trans_type[1].payee_name,
                    account2: res.extra.trans_type[2].account,
                    info: res.extra.trans_type,
                    bank_name: res.extra.trans_type[2].bank_name
                });
                for (var i = 0; i < info.length; i++) {
                    if (info[i].status == 1) {
                        that.setData({
                            choose: i + 1
                        });
                        break;
                    }
                }
            },
            fail: res => {}
        });
    },
    //获取tempIds
    getTempIds: function() {
        var that = this;
        common.request({
            endpoint: "set/template_get",
            method: "post",
            data: {},
            header: {
                "content-type": "application/json"
            },
            success: res => {
                let tmplIds = [];
                let subscribeMessage = true;
                res.extra.map(v => {
                    if (v.type == 32 && v.status == "1") {
                        tmplIds.push(v.template_id);
                    }
                });
                if (!tmplIds.length) {
                    subscribeMessage = false;
                }
                that.setData({
                    tmplIds: tmplIds,
                    subscribeMessage: subscribeMessage
                });
            }
        });
    }
});